//
//  JKCommentRefModel.swift
//  ZhiHuDaily-Swift
//
//  Created by albert on 2018/3/12.
//  Copyright © 2018年 安永博. All rights reserved.
//

import UIKit

class JKCommentRefModel: JKBaseModel {
    /** 作者名 */
    var author: String?
    
    /** 内容 */
    var content: String?
    
    /** 内容 */
    var _contentRefAttr: NSAttributedString?
    var contentRefAttr: NSAttributedString {
        
        if _contentRefAttr == nil {
            
            let para = NSMutableParagraphStyle()
            para.lineSpacing = 5
            
            // 1.创建可变属性字符串 self.refName” 显示黑色加粗
            let attrMstr = NSMutableAttributedString(string: "//" + (author ?? "") + ":  ", attributes: [NSAttributedStringKey.foregroundColor : UIColor.black, NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 13), NSAttributedStringKey.paragraphStyle : para])
            
            // 2.创建不可变属性字符串 reply_to.content 显示灰色
            let attrStr = NSAttributedString(string: content ?? "", attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 14), NSAttributedStringKey.foregroundColor : JKSameRGBColor(108), NSAttributedStringKey.paragraphStyle : para])
            
            // 3.可变字符串拼接不可变字符串
            attrMstr.append(attrStr)
            
            _contentRefAttr = attrMstr
        }
        
        return _contentRefAttr!
    }
    
    /** 内容 夜间模式 */
    private var _contentRefNightAttr: NSAttributedString?
    var contentRefNightAttr: NSAttributedString {
        
        if _contentRefNightAttr == nil {
            
            let para = NSMutableParagraphStyle()
            para.lineSpacing = 5
            
            // 1.创建可变属性字符串 self.refName” 显示黑色加粗
            let attrMstr = NSMutableAttributedString(string: "//" + (author ?? "") + ":  ", attributes: [NSAttributedStringKey.foregroundColor : JKSameRGBColor(186), NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 13), NSAttributedStringKey.paragraphStyle : para])
            
            // 2.创建不可变属性字符串 reply_to.content 显示灰色
            let attrStr = NSAttributedString(string: content ?? "", attributes: [NSAttributedStringKey.foregroundColor : JKSameRGBColor(118), NSAttributedStringKey.paragraphStyle : para])
            
            // 3.可变字符串拼接不可变字符串
            attrMstr.append(attrStr)
            
            _contentRefNightAttr = attrMstr
        }
        
        return _contentRefNightAttr!
    }
    
    /** 状态 */
    var status: Int = 0
    
    /** 评论ID */
    var id: String?
    
    /** cmtContentRefH */
    var cmtContentRefH: CGFloat {
        
        if _cmtContentRefH <= 0 {
            
            let contentRefFont = UIFont.systemFont(ofSize: 14)
            
            // 展开收起按钮的显示和隐藏
            //计算文字的高度
            let maxSize = CGSize(width: JKScreenW - 30 - 20 - 8, height: CGFloat(MAXFLOAT))
            
            _cmtContentRefH = contentRefAttr.boundingRect(with: maxSize, options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil).size.height
            
            if _cmtContentRefH - contentRefFont.lineHeight <= 5 {
                
                if jk_containChinese(str: author) || jk_containChinese(str: content) {
                    
                    _cmtContentRefH -= 5
                    
                    // 1.创建可变属性字符串 self.refName” 显示黑色加粗
                    let attrMstr = NSMutableAttributedString(string: "//" + (author ?? "") + ":  ", attributes: [NSAttributedStringKey.foregroundColor : UIColor.black, NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 13)])
                    
                    // 2.创建不可变属性字符串 reply_to.content 显示灰色
                    let attrStr = NSAttributedString(string: content ?? "", attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 14), NSAttributedStringKey.foregroundColor : JKSameRGBColor(108)])
                    
                    // 3.可变字符串拼接不可变字符串
                    attrMstr.append(attrStr)
                    
                    _contentRefAttr = attrMstr
                    
                    // 1.创建可变属性字符串 self.refName” 显示黑色加粗
                    let attrMstr1 = NSMutableAttributedString(string: "//" + (author ?? "") + ":  ", attributes: [NSAttributedStringKey.foregroundColor : JKSameRGBColor(186), NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 13)])
                    
                    // 2.创建不可变属性字符串 reply_to.content 显示灰色
                    let attrStr1 = NSAttributedString(string: content ?? "", attributes: [NSAttributedStringKey.foregroundColor : JKSameRGBColor(118)])
                    
                    // 3.可变字符串拼接不可变字符串
                    attrMstr1.append(attrStr1)
                    
                    _contentRefNightAttr = attrMstr1
                }
            }
            
            _cmtContentRefH = ceil(_cmtContentRefH)
        }
        
        return _cmtContentRefH
    }
    
    /** cmtContentRefH */
    private var _cmtContentRefH: CGFloat = 0
}

